package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacGroupLife;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacGroupLifeService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 不认证的URL请求.
 *
 * @author paascloud.net @gmail.com
 */
@RestController
@RequestMapping(value = "/groupLife")
public class UacGroupLifeController extends BaseController {


	@Resource
	private UacGroupLifeService uacGroupLifeService;
	@Resource
	private CmnyMessageSource cmnyMessageSource;

	@PostMapping(value = "/selectOne")
	public Wrapper selectOne() {

		UacGroupLife uacGroupLife=uacGroupLifeService.selectOne(getLoginAuthDto().getGroupId());


		//return WrapMapper.ok(uacGroupLife);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), uacGroupLife);
	}


	@PostMapping(value = "/add")
	public Wrapper add(@RequestBody UacGroupLife uacGroupLife) {

		if(PublicUtil.isNotEmpty(uacGroupLifeService.selectOne(getLoginAuthDto().getGroupId()))){
			return WrapMapper.error("机构已经存在信息");
		}

		uacGroupLife.setGroupId(getLoginAuthDto().getGroupId());
		Integer result=uacGroupLifeService.add(uacGroupLife);

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

	@PostMapping(value = "/update")
	public Wrapper update(@RequestBody UacGroupLife uacGroupLife) {

		uacGroupLife.setGroupId(getLoginAuthDto().getGroupId());
		Integer result=uacGroupLifeService.update(uacGroupLife);

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}
}
